package com.controller;

import com.bean.Admin;
import com.service.AdminService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class AdminController {
    @Resource
    AdminService adminService;

    /***
     * 该接口并未使用
     *
     * @return Login视图
     */
    @RequestMapping(value = "/toLogin")
    public String initLogin() {
        return "Login";
    }

    @RequestMapping(value = "/Login", method = RequestMethod.POST)
    public String Login(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session, HttpServletRequest request, Model model) {
        Admin admin = adminService.checkAdmin(username, password);
        if (admin == null) {
            request.setAttribute("msg", "用户名或密码错误");
            model.addAttribute("msg","用户名或密码错误");
//            return "redirect:/toLogin";
            return "Login";
        } else if (adminService.checkAdmin(username, password) != null && password.equals(admin.getPassword()) && username.equals(admin.getUsername())) {
            session.setAttribute("admin", admin);
            return "redirect:/user/listUser";
        }
        return "redirect:/toLogin";
    }


    @RequestMapping(value = "/logOut", method = RequestMethod.GET)
    public String logOut(HttpSession session) {
        session.invalidate();
        return "Login";
    }
}
